const httpUtils = require('../../../../utils/httpUtils')
const ui = require('../../../../utils/ui')
const util = require('../../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: ['居民身份证', '军官证', '居民户口簿', '港澳居民来往内地通行证', '台湾居民来往内地通行证', '外国护照', '中国护照', '出生医学证明', '其他法定有效证件'],
    arrIndex: 0,
    relation: ['本人', '其他'],
    relationIndex: 0,
    nations: ["汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族",
      "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族",
      "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族",
      "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"],
    nationsIndex: 0,
    next: true,
    card: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let cardId = options.card_id
    console.log(cardId)
    this.getCardById(cardId)
  },
  getCardById(id) {
    let _this = this
    //发送请求
    httpUtils.request({
      method: "GET",
      showLoading: true,
      url: `/api/card/detail/` + id,
      message: "正在查询..."
    }).then(res => {
      if (res.data.code != 0) {
        ui.showToast(res.data.mes)
      } else {
        console.log(res.data.data)
        for (let i = 0; i < _this.data.nations.length; i++) {
          if (res.data.data.nation == _this.data.nations[i]) {
            _this.setData({
              nationsIndex: i
            })
          }
        }
        for (let i = 0; i < _this.data.arr.length; i++) {
          if (res.data.data.id_type == _this.data.arr[i]) {
            _this.setData({
              arrIndex: i
            })
          }
        }
        res.data.data.id_number = util.encryptIdNo(res.data.data.id_number)
        _this.setData({
          card: res.data.data,
          relationIndex: res.data.data.relationship
        })
      }
    }).catch(err => {
      console.log('ERROR')
    });
  },
  nextToTrue() {

    this.data.card.id_type = this.data.arr[this.data.arrIndex]
    this.data.card.nation = this.data.nations[this.data.nationsIndex]
    if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(this.data.card.name))) {
      ui.showToast("姓名有误")
      return
    }
    if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(this.data.card.id_number))) {
      ui.showToast("身份证号码有误")
      return false;
    }
    this.setData({
      next: true
    })
  },
  toSubmit() {
    console.log(this.data.card)
    let _this = this
    this.data.card.relationship = this.data.relationIndex
    if (!(/^1[34578]\d{9}$/.test(this.data.card.phone))) {
      ui.showToast("手机号码有误")
      return false;
    }
    let myCard = _this.data.card
    myCard.relationship = Number(myCard.relationship)
    //发送请求
    httpUtils.request({
      method: "POST",
      showLoading: true,
      url: `/api/card/update/` + _this.data.card.id,
      message: "正在创建...",
      data: myCard,
    }).then(res => {
      if (res.data.code != 0) {
        ui.showToast(res.data.mes)
      } else {
        wx.switchTab({
          url: '../my/index',
        })
      }
    }).catch(err => {
      console.log('ERROR')
    });
    console.log(this.data.card)
  },
  bindKeyInput(e) {
    this.setData({
      [`${e.currentTarget.dataset.params}`]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  submitInfo(e) {
    this.setData({
      formId: e.detail.formId,
    },
      (res) => { })
  },

  pickerChange(value) {
    console.log(value)
    this.setData({
      index: value.detail.value
    })
  },
  arrPickerChange(value) {
    this.setData({
      arrIndex: value.detail.value
    })
  },
  nationsPickerChange(value) {
    this.setData({
      nationsIndex: value.detail.value
    })
  },
  relationPickerChange(value) {
    this.setData({
      relationIndex: value.detail.value
    })
  },
  /**
   * 选择图片
   */
  onChooseFile() {
    wx.chooseImage({
      success: (resp) => {
        let imgPath = resp.tempFilePaths;
        console.log(imgPath);
        this.setData({ path: imgPath, hasFile: true });
      }
    })
  }
})
